package com.chenchao.javacommonclass;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/11 18:45
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.commonclass.StringTest2.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 可变长字符串：不会创建新的内存地址
 * StringBuilder:不加锁，更快
 * StringBuffer:synchronized：加锁，更安全
 */
public class StringTest2 {
    public static void main(String[] args) {
        testString();//312
        testStringBuilder();//0
        testStringBuffer();//1
    }
    public static void testString(){
        long time1=System.currentTimeMillis();
        String str="";
        for (int i = 0; i < 10000; i++) {
            str+=i;
        }
        System.out.println(System.currentTimeMillis()-time1);
    }
    public static void testStringBuilder(){
        long time1=System.currentTimeMillis();
        StringBuilder str=new StringBuilder();
        for (int i = 0; i < 10000; i++) {
            str.append(i);
        }
        System.out.println(System.currentTimeMillis()-time1);
    }
    public static void testStringBuffer(){
        long time1=System.currentTimeMillis();
        StringBuffer str=new StringBuffer();
        for (int i = 0; i < 10000; i++) {
            str.append(i);
        }
        System.out.println(System.currentTimeMillis()-time1);
    }
}
